package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductConverter;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @author Administrator
 *
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductConverter productConverter;


    @Override
    public PageVO<ProductVO> findProductList(Integer pageNum,
                                             Integer pageSize,
                                             String name,
                                             String categoryId,
                                             String supplier,
                                             Integer status,
                                             Long[] categorys,
                                             ProductVO productVO) {

        PageHelper.startPage(pageNum,pageSize);

        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        List<Product> products=null;

        if (name!=null&&!"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if (categoryId!=null&&!"".equals(categoryId)){
            criteria.andLike("pNum","%"+categoryId+"%");
        }
        if (supplier!=null&&!"".equals(supplier)){
            criteria.andLike("supplier","%"+supplier+"%");
        }
        if (status!=null&&!"".equals(status)){
            criteria.andEqualTo("status",""+status);
        }

        if (categorys!=null&&!"".equals(categorys)){
            int length = categorys.length;
            if (length>0){
                criteria.andEqualTo("oneCategoryId",""+categorys[0]);
            }
            if (length>1){
                criteria.andEqualTo("twoCategoryId",""+categorys[1]);
            }
            if (length>2){
                criteria.andEqualTo("threeCategoryId",""+categorys[2]);
            }

        }



         products = productMapper.selectByExample(example);

        List<ProductVO> productVOS = productConverter.ProductVOList(products);



        PageInfo<Product> info=new PageInfo<>(products);
        return new PageVO<ProductVO>(info.getTotal(),productVOS);
    }


    /*添加物资*/

    @Override
    public Boolean add(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString());
        product.setId(productVO.getId());


        Long[] categoryKeys = productVO.getCategoryKeys();

        if (categoryKeys.length>0){
            product.setOneCategoryId(categoryKeys[0]);
        }
        if (categoryKeys.length>1){
            product.setTwoCategoryId(categoryKeys[1]);
        }
        if (categoryKeys.length>2){
            product.setThreeCategoryId(categoryKeys[2]);
        }
        int insert = productMapper.insert(product);


        if (insert>0){
            return true;
        }
        return false;
    }

    /*编辑物资资料前回显数据*/
    @Override
    public ProductVO edit(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        return productVO;
    }

    //更新数据

    @Override
    public Boolean update(ProductVO productVO) {
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setModifiedTime(new Date());
        product.setId(productVO.getId());

        Long[] categoryKeys = productVO.getCategoryKeys();

        if (categoryKeys.length>0){
            product.setOneCategoryId(categoryKeys[0]);
        }
        if (categoryKeys.length>1){
            product.setTwoCategoryId(categoryKeys[1]);
        }
        if (categoryKeys.length>2){
            product.setThreeCategoryId(categoryKeys[2]);
        }

        int isUpdate = productMapper.updateByPrimaryKey(product);


        if (isUpdate>0){
            return true;
        }
        return false;
    }

    //将物资移入回收站

    @Override
    public Boolean remove(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(1);
        int i = productMapper.updateByPrimaryKey(product);

        if (i>0){
            return true;
        }
        return false;
    }

    /*将物资从回收箱里面恢复*/
    @Override
    public Boolean back(Integer id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        int i = productMapper.updateByPrimaryKey(product);

        if (i>0){
            return true;
        }
        return false;
    }

    /*从数据库中删除数据*/
    @Override
    public Boolean delete(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        int i = productMapper.deleteByPrimaryKey(product);
        if (i>0){
            return true;
        }
        return false;
    }

    /*从审核表中通过审核*/
    @Override
    public Boolean publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        product.setStatus(0);
        int i = productMapper.updateByPrimaryKey(product);

        if (i > 0) {
            return true;
        }
        return false;
    }

}
